package com.springapi.controller;

import com.springapi.entity.Orderlist;
import com.springapi.service.OrderlistService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 订单明细表(Orderlist)表控制层
 *
 * @author makejava
 * @since 2024-12-31 14:13:37
 */
@RestController
@RequestMapping("/api/orderlist")
public class OrderlistController {
    /**
     * 服务对象
     */
    @Resource
    private OrderlistService orderlistService;


    @GetMapping("/getorderlistbyday")
    public ResponseEntity<List<Orderlist>> getorderlistbyday(@RequestParam int appid, @RequestParam int shopid, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date createdate1, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date createdate2) {
        return ResponseEntity.ok(orderlistService.getorderlistbyday(appid,shopid,createdate1,createdate2));
    }


    /**
     * 分页查询
     *
     * @param orderlist 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Orderlist>> queryByPage(Orderlist orderlist, PageRequest pageRequest) {
        return ResponseEntity.ok(this.orderlistService.queryByPage(orderlist, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Orderlist> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.orderlistService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param orderlist 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Orderlist> add(@RequestBody Orderlist orderlist) {
        return ResponseEntity.ok(this.orderlistService.insert(orderlist));
    }


    /**
     * 批量新增数据
     *
     * @param orderlist 实体对象数组
     * @return 新增结果
     */
    @PostMapping("/addall")
    public ResponseEntity<String> addall(@RequestBody List<Orderlist> orderlists) {
        return ResponseEntity.ok(this.orderlistService.insertBatchorder(orderlists));
    }


    /**
     * 编辑数据
     *
     * @param orderlist 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Orderlist> edit(Orderlist orderlist) {
        return ResponseEntity.ok(this.orderlistService.update(orderlist));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.orderlistService.deleteById(id));
    }

}

